using System;
using System.Data;
using System.Security.Permissions;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
using System.Collections;

using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Arfila.Web.Logic
{
    //This Virtual Path Provider (VPP) excludes all .css files (Cascading Style Sheets) 
    // from the App_Theme directory with the exception of the Import.css file.

    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.High)]
    public class CustomVirtualPathProvider : VirtualPathProvider
    {
        public static void AppInitialize()
        {
            HostingEnvironment.RegisterVirtualPathProvider(new CustomVirtualPathProvider());
        } 

        public CustomVirtualPathProvider() : base() { }

        private bool IsThemeDirectory(string virtualPath)
        {
            String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
            return checkPath.StartsWith("~/App_Themes", StringComparison.InvariantCultureIgnoreCase);
        }

        public override VirtualDirectory GetDirectory(string virtualDir)
        {
            if (IsThemeDirectory(virtualDir))
            {
                return new ThemeDirectory(Previous.GetDirectory(virtualDir));
            }
            else
            {
                return Previous.GetDirectory(virtualDir);
            }
        }
    }

    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class ThemeDirectory : VirtualDirectory
    {
        VirtualDirectory _toFilter;
        private ArrayList _children = new ArrayList();
        private ArrayList _directories = new ArrayList();
        private ArrayList _files = new ArrayList();

        public override IEnumerable Children
        {
            get { return _children; }
        }

        public override IEnumerable Directories
        {
            get { return _directories; }
        }

        public override IEnumerable Files
        {
            get { return _files; }
        }

        public ThemeDirectory(VirtualDirectory toFilter) : base(toFilter.VirtualPath)
        {
            _toFilter = toFilter;
            BuildChild();
        }

        private void BuildChild()
        {
            foreach (VirtualDirectory dirToFilter in _toFilter.Directories)
            {
                ThemeDirectory themeDir = new ThemeDirectory(dirToFilter);

                _children.Add(themeDir);
                _directories.Add(themeDir);
            }
            foreach (VirtualFile fileToFilter in _toFilter.Files)
            {
                string fileExtension = VirtualPathUtility.GetExtension(fileToFilter.VirtualPath).TrimStart('.');
                string fileNameExeption = "Import.css";
                if (string.Compare(fileExtension, "css", true) != 0 || string.Compare(fileToFilter.Name, fileNameExeption, true) == 0)
                {
                    _children.Add(fileToFilter);
                    _files.Add(fileToFilter);
                }
                else {}
            }
        }
    }
}